package com.suo.web;

import com.alibaba.fastjson.JSON;
import com.suo.mapper.BookMapper;
import com.suo.pojo.Book;
import com.suo.pojo.Cart;
import com.suo.pojo.CartItem;
import com.suo.pojo.User;
import com.suo.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


public class CartServlet extends BaseServlet {

    /**
     * 将商品添加到购物车
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");

        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BookMapper mapper = sqlSession.getMapper(BookMapper.class);

        Book book = mapper.selectById(Integer.parseInt(id));

        CartItem item = new CartItem(book.getId(),book.getName(),1,new BigDecimal(book.getPrice()),new BigDecimal(book.getPrice()));

        Cart cart = (Cart) request.getSession().getAttribute("cart");

        if(cart == null) {
            cart = new Cart();
            cart.addItem(item);
            request.getSession().setAttribute("cart",cart);
        }
        else {
            cart.addItem(item);
        }

        sqlSession.close();

        System.out.println(cart);

        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void ajaxAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");

        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BookMapper mapper = sqlSession.getMapper(BookMapper.class);

        Book book = mapper.selectById(Integer.parseInt(id));

        CartItem item = new CartItem(book.getId(),book.getName(),1,new BigDecimal(book.getPrice()),new BigDecimal(book.getPrice()));

        Cart cart = (Cart) request.getSession().getAttribute("cart");

        if(cart == null) {
            cart = new Cart();
            cart.addItem(item);
            request.getSession().setAttribute("cart",cart);
        }
        else {
            cart.addItem(item);
        }

        sqlSession.close();

//        String s = JSON.toJSONString(cart);
//
//        int t = cart.getTotalCount();
//
//        String s1 = "" + t;
//
//        System.out.println(s);

//        User user = new User(1,"物","b","c");
//
//        String s2 = JSON.toJSONString(user);
//
//        System.out.println(s2);
//
        Map<String,Integer> map = new HashMap<>();
        map.put("totalCount",cart.getTotalCount());
        String s = JSON.toJSONString(map);

        response.getWriter().write(s);
    }


    /**
     * 将商品从购物车中删除
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        cart.deleteItem(Integer.parseInt(id));
        response.sendRedirect(request.getHeader("Referer"));
    }

    /**
     * 将商品从购物车中清空
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        cart.clear();
        response.sendRedirect(request.getHeader("Referer"));
    }

    /**
     * 将商品数量从购物车中更新
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String count = request.getParameter("count");
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        cart.updateCount(Integer.parseInt(id),Integer.parseInt(count));
        response.sendRedirect(request.getHeader("Referer"));
    }
}
